package com.example.demo.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.example.demo.entity.UserEntity;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.ExcelExportUtil;
import com.example.demo.vo.UserPageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DemoService {

    @Resource
    private UserMapper userMapper;

    public List<UserEntity> pageUser(UserPageVO userPageVO) {
        Pagination pagination = userPageVO.toPagination();
        EntityWrapper<UserEntity> wrapperDecorator = new EntityWrapper<>();
        return userMapper.selectPage(pagination, wrapperDecorator);
    }


    public byte[] excelExportByHSSF() throws IOException {
        ExcelExportUtil excelExport = new ExcelExportUtil();
        excelExport.setHeadKey(this.setHeadKey());
        excelExport.setHeadList(this.setHeadList());
        excelExport.setData(this.setData());
        excelExport.setFontSize(14);
        return excelExport.excelExportByHSSFForWeb();
    }

    public byte[] excelExportByXSSF() throws IOException {
        ExcelExportUtil excelExport = new ExcelExportUtil();
        excelExport.setHeadKey(this.setHeadKey());
        excelExport.setHeadList(this.setHeadList());
        excelExport.setData(this.setData());
        excelExport.setFontSize(14);
        return excelExport.excelExportByXSSFForWeb();
    }

    public byte[] excelExportBySXSSF() throws IOException {
        ExcelExportUtil excelExport = new ExcelExportUtil();
        excelExport.setHeadKey(this.setHeadKey());
        excelExport.setHeadList(this.setHeadList());

        //分页默认参数
        int pageNum = 1;//页数
        int pageSize = 100000;//页数大小

        UserPageVO userPageVO = new UserPageVO();
        userPageVO.setPageNumber(pageNum);
        userPageVO.setPageSize(pageSize);

        //总导出数据
        List<Map> allMapList = new ArrayList<>();
        //每次分页查询的数据，即临时数据
        List<UserEntity> userList = new ArrayList<>();

        //遍历查询数据，每次10万，避免一次性查询100万数据比较慢，影响性能
        do {
            //清空临时数据
            userList.clear();
            // 动态设置分页的页数
            userPageVO.setPageNumber(pageNum);
            // 分页查询数据
            userList = this.pageUser(userPageVO);

            //转换数据
            List<Map> _mapList = new ArrayList<>();
            for (UserEntity user: userList) {
                Map _map = new HashMap();
                _map.put("id", user.getId());
                _map.put("userName", user.getUserName());
                _map.put("nickName", user.getNickName());
                _mapList.add(_map);
            }

            //合并数据
            allMapList.addAll(_mapList);

            //页码+1
            pageNum++;

            //每次查询10万，查询10次即100万
            if (pageNum > 10 || userList.size() == 0){
                break;
            }
        } while (true);

        excelExport.setData(allMapList);
        excelExport.setFontSize(14);
        return excelExport.excelExportBySXSSFForWeb();
    }

    /**
     * 需要填充的数据信息
     * @return
     */
    private List<Map> setData(){
        List<Map> dataList = new ArrayList<>();
        Map map1 = new HashMap();
        map1.put("id", 1);
        map1.put("userName", "zhangsan");
        map1.put("nickName", "小三");
        Map map2 = new HashMap();
        map2.put("id", 2);
        map2.put("userName", "lisi");
        map2.put("nickName", "小四");
        dataList.add(map1);
        dataList.add(map2);
        return dataList;
    }

    /**
     * 表头数据的对应的key值
     * @return
     */
    private String[] setHeadKey(){
        String[] headKey = new String[]{"id","userName","nickName"};
        return headKey;
    }

    /**
     * 表头数据
     * @return
     */
    private String[] setHeadList(){
        String[] headList = new String[]{"ID","用户名","昵称"};
        return headList;
    }

}
